package com.atfcm.link;

/**
 * @Author 96534
 * @create 2024/11/17 23:16
 */
public class T19 {
     public ListNode removeNthFromEnd(ListNode head, int n) {
        if(head == null) return null;
        ListNode nummy = new ListNode(-1);
        nummy.next = head;

        ListNode slow = nummy;
        ListNode fast = nummy;

        while (n > -1 && fast != null) {
            fast = fast.next;
            n--;
        }

        while (fast != null) {
            fast = fast.next;
            slow = slow.next;
        }

        slow.next = slow.next.next;

        return nummy.next;

    }

    class ListNode {
        public int val;
        public ListNode next;

        ListNode(int val) {
            this.val = val;
        }
    }
}
